import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

path = r'f:\chromedriver.exe'

list = []
for line in open("list.txt","r",encoding="UTF-8"):   #设置文件对象
    ars = line.split('\t')
    are = {}
    are['name']=ars[0]
    are['url']=ars[1]
    list.append(are)

for e in list:

    chrome_options = Options()
    # 后面的两个是固定写法 必须这么写
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    # 驱动路径 谷歌的驱动存放路径
    path = r'f:\chromedriver.exe'
    # 创建浏览器对象
    browser = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)
    url =e['url']
    browser.get(url)
    browser.switch_to_frame("goto")
    # browser.switch_to.default_content()
    js = '''var obj = $("a[class=r_tit_a][onclick^=showBszn]");
        for(var i = 0;i<obj.length;i++)
        {
            var a ='';	var b ='';	var c ='';	var d ='';
            a = obj[i].getAttribute('onclick').split(",")[0].replace("showBszn('","").replace("'","");
            b = obj[i].getAttribute('onclick').split(",")[1].replace("'","").replace("'","");
            c = obj[i].getAttribute('onclick').split(",")[2].replace("'","").replace("'","").replace(")","");;
            if(obj[i].getAttribute('onclick').split(",").length>3)
            d = obj[i].getAttribute('onclick').split(",")[3].replace("'","").replace("'","").replace(")","");;
            //console.log(obj[i].title+'\t'+a+'\t'+b+'\t'+c+'\t'+d);
            console.log(obj[i].title+'\t'+'http://www.jszwfw.gov.cn/jszwfw/bscx/itemlist/ywList.do?webId=1&iddept_ql_inf='+b+'&iszx='+c);
        }'''
    # re = browser.execute_script('$("a[class=r_tit_a][onclick^=showBszn]")')
    res = []
    ars = browser.find_elements_by_class_name('r_tit_a')
    for ar in ars:
        # print(ar)
        oc = ar.get_attribute('onclick')
        if oc!=None:
            aroc = oc.split('\'')
            # print(aroc)
            url = 'http://www.jszwfw.gov.cn/jszwfw/bscx/itemlist/ywList.do?webId='+aroc[1]+'&iddept_ql_inf='+aroc[3]+'&iszx='+aroc[5]
            name = ar.get_attribute('title')
            # browser.implicitly_wait(5000)ii
            # html = browser.page_source
            # html = browser.execute_script("return document.documentEle8ioment.outerHTML")
            # print(html)
            re = {}
            re['url']=url
            re['name']=name
            res.append(re)
    browser.quit()
    with open("re/"+e['name']+".txt", "w",encoding='utf-8') as f:
        for re in res:
            f.writelines(re['name']+'\t'+re['url']+'\n')
